package com.dodock.android.banglapapers.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.dodock.android.banglapapers.BanglaNews;
import com.dodock.android.banglapapers.MyApplication;
import com.dodock.android.banglapapers.R;
import com.dodock.android.banglapapers.Utility;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;

public class SendToFriendActivity extends Activity {

	private TextView mailTo;
	private TextView cc_bcc;
	private TextView body;
	private TextView subject;
	private String message;
	
	private Tracker mGaTracker;
	private GoogleAnalytics mGaInstance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				
		//Google Analytics
		try{
		    mGaInstance = GoogleAnalytics.getInstance(this);
		    mGaTracker = mGaInstance.getTracker(getResources().getString(R.string.ga_trackingId));
		}
		catch (Exception e) {
		}
		//--Google Analytics
		
		
		setContentView(R.layout.send_mail);

		Button send = (Button) findViewById(R.id.send);
		send.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				sendMailToFriend();
			}
		});

		Button cancel = (Button) findViewById(R.id.cancle);
		cancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		mailTo = (TextView) findViewById(R.id.mail_to);
		cc_bcc = (TextView) findViewById(R.id.cc_bcc);
		body = (TextView) findViewById(R.id.body);
		subject = (TextView) findViewById(R.id.subject);
		try{
		message = "<p>"
				+ BanglaNews.configuration.getSendToFriendText()
				+ "</p>"
				+ "<a href=\"http://www.stackoverflow.com\">Download BanglaNews for Android</a>"
				+ "<p>Sent from my andorid</p>";
		}
		catch (Exception e) {
			message = "<p>"
					+ "</p>"
					+ "<a href=\"http://www.stackoverflow.com\">Download BanglaNews for Android</a>"
					+ "<p>Sent from my andorid</p>";
		}
		body.setText(Html.fromHtml(message));
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		com.facebook.Settings.publishInstallAsync(this, MyApplication.fb_app_id);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		try {
			mGaTracker.sendView("/SendToFriend");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
	
	private void sendMailToFriend() {
		// TODO Auto-generated method stub
		String to = mailTo.getText().toString();
		String cc = cc_bcc.getText().toString();
		if (!to.equalsIgnoreCase("")) {
			if (Utility.isEmailAddressValid(to)) {
				if (!cc.equalsIgnoreCase(""))
					if (!Utility.isEmailAddressValid(cc)) {
						showErrorMessage();
						return;
					}
				final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
				emailIntent.setType("text/html");
				emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
				emailIntent.putExtra(Intent.EXTRA_CC, new String[] { cc });
				emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject.getText()
						.toString());
				emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(message));
				startActivity(Intent.createChooser(emailIntent, "Email:"));
				
			} else {
				showErrorMessage();
			}
		}
	}

	private void showErrorMessage() {
		final AlertDialog.Builder alert = new AlertDialog.Builder(
				SendToFriendActivity.this);
		alert.setTitle("Email address");
		alert.setMessage("Email address is not valid");
		alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		});
		alert.show();
	}

}
